<?php
// +----------------------------------------------------------------------
// | ThinkCMF [ WE CAN DO IT MORE SIMPLE ]
// +----------------------------------------------------------------------
// | Copyright (c) 2013-2017 http://www.thinkcmf.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: Dean <zxxjjforever@163.com>
// +----------------------------------------------------------------------
namespace api\api\service;


use think\Db;

class MyService
{
    /**获取用户余额
     * @param $user_id 用户
     * @return array|false|\PDOStatement|string|\think\Collection
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function user_balance($user_id)
    {
        $balance = Db::name('user')->where('id', $user_id)->value('balance');
        return $balance;

    }

    /**获取支付日志
     * @param $userid
     * @param $page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function get_user_balance_log($userid, $page)
    {
        $balance_list = Db::name('user_balance_log')->where(array('user_id' => $userid,'usertype'=>1))->field("*,from_unixtime(create_time,'%Y-%m-%d') as c_time")->page($page, 10)->order('create_time', 'desc')->select()->toArray();
        return $balance_list;
    }

    /**获取订单通知
     * @param $userid
     * @param $page
     * @return array
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    function get_user_meaaage_log($userid,$page,$type)
    {
        $balance_list = Db::name('user_message')->where(array('user_id' => $userid,'type'=>$type))->page($page, 10)->order('timeint', 'desc')->select()->toArray();
        return $balance_list;
    }

    function get_user_meaaage_detail($userid, $id){
        $balance_list = Db::name('user_message')->where(array('user_id' => $userid,'user_msg_id'=>$id))->find();
        if($balance_list){
            Db::name('user_message')->where(array('user_id' => $userid,'user_msg_id'=>$id))->update(array('is_read'=>1));
        }
        return $balance_list;
    }
	/**获取系统通知
	 * @param $userid
	 * @param $page
	 * @return array
	 * @throws \think\db\exception\DataNotFoundException
	 * @throws \think\db\exception\ModelNotFoundException
	 * @throws \think\exception\DbException
	 */
	function get_user_meaaage_sys_log($userid, $page = 1,$utype)
	{
	    $msg_sys_obj  = Db::name('message_sys')->where(array('uid' => $userid, 'message_type' => $utype))->find();
		if ($msg_sys_obj) {
			$unread_msgids = $msg_sys_obj['unread_msgids'];
			$msg_objs = Db::name('message')->where('message_type=' . $utype)->page($page, 10)->order('time desc')->select();//查询公告表
			$count = Db::name('message_sys')->where('message_type=' . $utype)->count();
			if ($msg_objs) {
				$msg_objs_data = [];
				foreach ($msg_objs as $key => $value) {
					$msg_objs_data[$key] = $value;
					$message_id = $value['id'];
					$unread_arr = explode(",", $unread_msgids);
					$search_ret = array_search($message_id, $unread_arr);
					if ($search_ret === false) {
						$msg_objs_data[$key]['is_read'] = 1;
					} else {
						$msg_objs_data[$key]['is_read'] = 0;
					}
				}
				$rets['data'] = $msg_objs_data;
				$rets['count'] = ceil($count / 10);
				return $rets;
			} else{
				return null;
			}
		} else {
			return null;
		}
	}
	
	function get_user_meaaage_sys_detail($userid, $message_id){
	    $msg_obj  = Db::name('message')->field('id,message_type,content')->where(array('id' => $message_id))->find();
		if ($msg_obj) {
			$sys_obj = Db::name('message_sys')->where(array('uid' => $userid, 'message_type' => $msg_obj['message_type']))->find();
			$unread_msgids = $sys_obj['unread_msgids'];
			$unread_arr = explode(",", $unread_msgids);
			$search_ret = array_search($message_id, $unread_arr);
			if ($search_ret === false) {
				return htmlspecialchars_decode($msg_obj['content']);
			} else {
				unset($unread_arr[$search_ret]);
				$new_unread_str = implode(',', $unread_arr);
				if ($sys_obj['read_msgids']) {
					$data['read_msgids'] = $sys_obj['read_msgids'] . ',' . $message_id;
					$data['read_counts'] = $sys_obj['read_counts'] + 1;
				} else {
					$data['read_msgids'] = $message_id;
					$data['read_counts'] = 1;
				}
				$data['unread_msgids'] = $new_unread_str;
				$data['unread_counts'] = $sys_obj['unread_counts'] - 1;
				$data['read_update_time'] = date('Y-m-d H:i;s', time());
				$save_ret = Db::name('message_sys')->where('sysmsg_id=' . $sys_obj['sysmsg_id'])->update($data);
				return htmlspecialchars_decode($msg_obj['content']);
				if ($save_ret) {
					return true;
				} else{
					return false;
				}
			}
		}
	}
}
